package io.renren.modules.sys.service.impl;

import io.renren.modules.sys.vo.CategoryVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.DormAreaDao;
import io.renren.modules.sys.entity.DormAreaEntity;
import io.renren.modules.sys.service.DormAreaService;


@Service("dormAreaService")
public class DormAreaServiceImpl extends ServiceImpl<DormAreaDao, DormAreaEntity> implements DormAreaService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<DormAreaEntity> wrapper = new QueryWrapper<>();
        String key = (String)params.get("key");
        wrapper.like(StringUtils.isNotBlank(key), "name",key);


        if (params.get("level")!=null || ! params.get("level").equals("")){
            int level = Integer.parseInt((String) params.get("level"));
            wrapper.eq("level", level);
        }

        IPage<DormAreaEntity> page = this.page(
                new Query<DormAreaEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<DormAreaEntity> getCatelogy() {
//        List<DormAreaEntity> list = this.list();
//        List<CateloryVo> list1=new ArrayList<>();
//        for (DormAreaEntity entity : list) {
//            if (entity.getLevel()==0){
//                CateloryVo cateloryVo = new CateloryVo();
//                BeanUtils.copyProperties(entity, cateloryVo);
//                list.add(entity);
//                list.remove(entity);
//            }
//        }
//        for (DormAreaEntity entity : list) {
//            if (entity.getLevel()==1){
//                CateloryVo cateloryVo = new CateloryVo();
//                BeanUtils.copyProperties(entity, cateloryVo);
//                for (CateloryVo vo : list1) {
//                    if (vo.getId().equals(entity.getParentId())){
//                        vo.setChildren();
//                    }
//                }
//                list.add(entity);
//                list.remove(entity);
//            }
//        }
//        return new CateloryVo[0];

        List<DormAreaEntity> entities = baseMapper.selectList(null);

        //2、组装成父子的树形结构

        //2.1）、找到所有的一级分类
        List<DormAreaEntity> level1Menus = entities.stream().filter(categoryEntity ->
                categoryEntity.getLevel() == 0
        ).map((menu) -> {
            menu.setChildren(getChildrens(menu, entities));
            return menu;
        }).collect(Collectors.toList());
        return level1Menus;
    }

    //递归查找所有菜单的子菜单
    private List<DormAreaEntity> getChildrens(DormAreaEntity root, List<DormAreaEntity> all) {

        List<DormAreaEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId().equals( root.getId());
        }).map(categoryEntity -> {
            //1、找到子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity, all));
            return categoryEntity;
        }).collect(Collectors.toList());
        return children;
    }

}